package com.example.teachmvandroidchart.util

import android.content.Context
import android.net.ConnectivityManager
import android.net.ParseException
import android.os.Build
import android.util.DisplayMetrics
import android.util.Log
import android.view.Window
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author geyifeng
 * @date 2019/4/14 4:59 PM
 */
class Utils private constructor() {

    companion object {
        private var context: Context? = null
        fun Endtime(str: String?): String {
            val sb = StringBuilder(str)
            Log.d("字符串：", "" + sb.toString() + " 字符长度：" + sb.length)
            sb.insert(10, "T")
            return sb.toString()
        }

        fun Starttime(str: String?): String {
            val sb = StringBuilder(str)
            Log.d("字符串：", "" + sb.toString() + " 字符长度：" + sb.length)
            sb.insert(10, "T")
            sb.insert(16, ":30")
            return sb.toString()
        }

        /**
         * 初始化工具类
         *
         * @param context 上下文
         */
        fun init(context: Context) {
            Companion.context = context.applicationContext
        }

        /**
         * 获取ApplicationContext
         *
         * @return ApplicationContext
         */
        fun getContext(): Context? {
            if (context != null) {
                return context
            }
            throw NullPointerException("u should init first")
        }

        /**
         * 判断str1中包含str2的个数
         * @param str1
         * @param str2
         * @return counter
         */
        var counter = 0
        fun countStr(str1: String, str2: String): Int {
            if (str1.indexOf(str2) == - 1) {
                return 0
            } else if (str1.indexOf(str2) != - 1) {
                counter ++
                countStr(
                    str1.substring(
                        str1.indexOf(str2) +
                                str2.length
                    ), str2
                )
                return counter
            }
            return 0
        }

        // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
        // data Date类型的时间
        fun dateToString(data: Date?, formatType: String?): String {
            return SimpleDateFormat(formatType).format(data)
        }

        // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
        // HH时mm分ss秒，
        // strTime的时间格式必须要与formatType的时间格式相同
        @Throws(ParseException::class, java.text.ParseException::class)
        fun stringToDate(strTime: String?, formatType: String?): Date? {
            val formatter = SimpleDateFormat(formatType)
            var date: Date? = null
            date = formatter.parse(strTime)
            return date
        }

        fun getWidthAndHeight(window: Window?): Array<Int?>? {
            if (window == null) {
                return null
            }
            val integer = arrayOfNulls<Int>(2)
            val dm = DisplayMetrics()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                window.windowManager.defaultDisplay.getRealMetrics(dm)
            } else {
                window.windowManager.defaultDisplay.getMetrics(dm)
            }
            integer[0] = dm.widthPixels
            integer[1] = dm.heightPixels
            return integer
        }

        val pic: String
            get() {
                val random = Random()
                return "http://106.14.135.179/ImmersionBar/" + random.nextInt(40) + ".jpg"
            }
        val pics: ArrayList<String>
            get() = getPics(4)

        fun getPics(num: Int): ArrayList<String> {
            val pics = ArrayList<String>()
            val random = Random()
            do {
                val s = "http://106.14.135.179/ImmersionBar/" + random.nextInt(40) + ".jpg"
                if (! pics.contains(s)) {
                    pics.add(s)
                }
            } while (pics.size < num)
            return pics
        }

        val fullPic: String
            get() {
                val random = Random()
                return "http://106.14.135.179/ImmersionBar/phone/" + random.nextInt(40) + ".jpeg"
            }

        fun isNetworkConnected(context: Context?): Boolean {
            if (context != null) {
                val manager = context
                    .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
                val mNetworkInfo = manager.activeNetworkInfo
                if (mNetworkInfo != null) {
                    return mNetworkInfo.isAvailable
                }
            }
            return false
        }
    }

    init {
        throw UnsupportedOperationException("u can't instantiate me...")
    }
}
